<?php
header("Content-type: text/html; charset=utf-8");

$page = $_GET['page']; // get the requested page 
$limit = $_GET['rows']; // get how many rows we want to have into the grid 
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
$sord = $_GET['sord']; // get the direction
$action = $_GET['action'];

$notify_type = 0;
switch($action) {
	case 'sms':
		$notify_type = 0;
		break;
	case 'email':
		$notify_type = 1;		
		break;
	default:
		die('Invalid action value !');
} 
$admin = true;
require_once('auth_req.inc.php');

$rs = $dbh->query('SELECT count(*) FROM msg_setup WHERE notify_type='. $notify_type);

if ($rec = $rs->fetchAll(PDO::FETCH_COLUMN, 0))
{
    $count = $rec[0];
}
else
{
    $count = 0;
}

if( $count > 0 ) 
{ 
    $total_pages = ceil($count/$limit); 
} 
else 
{ 
    $total_pages = 0; 
} 

if ($page > $total_pages) 
    $page=$total_pages;


$start = $limit * $page - $limit;

if ($start < 0) 
    $start = 0;

$sth = $dbh->prepare('SELECT ms_id, notify_info FROM msg_setup WHERE notify_type=:notify LIMIT :start,:limit');

$sth->bindParam(':notify', intval($notify_type), PDO::PARAM_INT);
$sth->bindParam(':start', intval($start), PDO::PARAM_INT);
$sth->bindParam(':limit', intval($limit), PDO::PARAM_INT);

$sth->execute();

$response->page = $page; 
$response->total = $total_pages; 
$response->records = $count; 

$alldb = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach( $alldb as $key => $val)
{
    $response->rows[$key]['id'] = $val['ms_id'];
   	$response->rows[$key]['cell'] = array( $val['ms_id'], $val['notify_info']);
}

echo json_encode($response);
?>